"use strict"

let mysql = require('mysql');

//填写数据库连接信息
let username = 'ug';
let password = 'ug';
let db_host = '172.16.7.12';
let db_port = 3306;
let db_name = 'ug';
let option = {
    host: db_host,
    port: db_port,
    user: username,
    password: password,
    database: db_name
};

let exec_batch = (sqlObjs, after, finalAfter) => {
    var client = mysql.createConnection(option);

    client.connect(function(err) {
        if (err) {
            console.log(err);
            return;
        }

        for (let sqlObj of sqlObjs) {
            let sql = sqlObj.sql || '';
            let values = sqlObj.values || [];
            console.log("**** sql: " + sql);
            console.log("**** params: " + obj2string(values));

            client.query(sql, values, (err, result) => {
                if (err) {
                    console.log(err);
                }

                if (typeof after === 'function') {
                    after(err, result);
                }
            });
        }

        if (typeof finalAfter === 'function') {
            finalAfter();
        }

        client.end();
    });
};

let exec = (sql, values, after) => {
    console.log("**** sql: " + sql);
    console.log("**** params: " + obj2string(values));

    var client = mysql.createConnection(option);
    client.connect(function(err) {
        if (err) {
            console.log(err);
            return;
        }

        client.query(sql || '', values || [], function(err, r) {
            if (err) {
                console.log(err);
            }
            if (typeof after === 'function') {
                after(err, r);
            }
        });
        client.end();
    });
    client.on('error', function(err) {
        if (err.errno != 'ECONNRESET') {
            if (typeof after === 'function') {
                after("err01", false);
            }
            throw err;
        } else {
            if (typeof after === 'function') {
                after("err02", false);
            }
        }
    });
};

let escape = param => {
    if (param === undefined || param === null || param === '') {
        return '';
    }
    return mysql.escape(param);
};

let paramFilter = param => {
    if (param === undefined || param === null || param === '') {
        return '';
    }
    var re = /select|update|delete|exec|count|'|"|=|;|>|<|%/i;
    if (re.test(param)) {
        return '';
    }
    return param;
};

/**
 * object 转 string
 * @param  {[type]} o [description]
 * @return {[type]}   [description]
 */
let obj2string = o => {
    if (o === null || o === undefined) {
        return '';
    }

    var r = [];
    if (typeof o == "string") {
        return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    }
    if (typeof o == "object") {
        if (!o.sort) {
            for (var i in o) {
                r.push(i + ":" + obj2string(o[i]));
            }
            if (!!document.all && !/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}";
        } else {
            for (var i = 0; i < o.length; i++) {
                r.push(obj2string(o[i]))
            }
            r = "[" + r.join() + "]";
        }
        return r;
    }
    return o.toString();
}

exports.exec = exec;
exports.exec_batch = exec_batch;
exports.escape = escape;
exports.paramFilter = paramFilter;